package com.turing.data.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @author hzh
 * @date 2022/11/29 11:53
 */
public class DuplicateUtil {


    /**
     * 过滤重复数据
     * @param keyExtractor  keyExtractor
     * @return {@link Predicate}<{@link T}>
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> map = new HashMap<>(16);
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

}

